using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LANamp
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private LanAmpClient Ampclient;
        private bool enlarged = false;
        public Form JoinForm;
        public LanAmpClient AmpClient
        {
            get { return Ampclient; }
            set { if (Ampclient == null) Ampclient = value; }
        }
        
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Width = 353;
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            Ampclient.ServerDisconnected += new LanAmpClient.ServerDisconnectedEvent(Ampclient_ServerDisconnected);
            Ampclient.ClientsUpdated += new ClientList.UpdateEvent(Ampclient_ClientsUpdated);
            labServerName.Text = Ampclient.ConnectedServer.Name;
            labServerHost.Text = Ampclient.ConnectedServer.Hostname;
            labServerVersion.Text = Ampclient.ConnectedServer.Version;
            labServerWinamp.Text = Ampclient.ConnectedServer.WinampVersion;
            this.Text = "LanAmp [" + Ampclient.Nickname + "]";
            niLanAmp.Text = "LanAmp [" + Ampclient.ConnectedServer.Name+ "]";
            Ampclient.NewSongInfo += new LanAmpClient.NewSongInfoEvent(Ampclient_NewSongInfo);
            Ampclient.PlayListUpdate += new LanAmpClient.PlayListUpdateEvent(Ampclient_PlayListUpdate);
            Ampclient.NewNextUser += new LanAmpClient.NewNextUserEvent(Ampclient_NewNextUser);
            Ampclient.NewSkipVote += new LanAmpClient.NewSkipVoteEvent(Ampclient_NewSkipVote);
            Ampclient.NewSkipVoteDone += new LanAmpClient.NewSkipVoteDoneEvent(Ampclient_NewSkipVoteDone);
        }

        void Ampclient_NewSkipVoteDone(string whoSkipvoted)
        {
            niLanAmp.ShowBalloonTip(2000, niLanAmp.Text, "User \"" + whoSkipvoted + "\" voted to skip this song\nNext user to select a song: "+labNextUser.Text, ToolTipIcon.Info);
        }

        public bool Enlarged
        {
            get { return enlarged; }
            set
            {
                enlarged = value;
                if (enlarged)
                {
                    btnEnlarge.Left = btnEnlarge.Left + 195;
                    this.Left = this.Left - 195;
                    this.Width = 548;
                    gbUsers.Visible = true;
                    gbClientInfo.Visible = true;
                }
                else
                {
                    this.Width = 353;
                    this.Left = this.Left + 195;
                    gbUsers.Visible = false;
                    gbClientInfo.Visible = false;
                    btnEnlarge.Left = btnEnlarge.Left - 195;
                }

            }
        }

        void Ampclient_NewSkipVote(int skipVote)
        {
            if (Ampclient.ConnectedServer != null)
            {
                int total = Ampclient.Clients.CountOnServer(Ampclient.ConnectedServer);
                float perc = (total > 0) ? (float)skipVote / (float)total : (float)0;
                labSkipVotes.Text = skipVote.ToString() + " / " + total.ToString() + " (" + (perc).ToString("p0") + ")";
                btnSkipVote.Enabled = (!Ampclient.SkipVote);
            }
        }

        void Ampclient_PlayListUpdate()
        {
            lbPlaylist.BeginUpdate();
            lbPlaylist.Items.Clear();
            foreach (MusicInfo song in Ampclient.Playlist.Items)
            {
                lbPlaylist.Items.Add(song);
            }
            lbPlaylist.SelectedItem = Ampclient.Playlist.SelectedItem;
            lbPlaylist.EndUpdate();
        }

        void Ampclient_NewNextUser(string nextUser)
        {
            labNextUser.Text = nextUser;
        }

        void Ampclient_NewSongInfo(string NewSongInfo)
        {
            labSong.Text = NewSongInfo.Replace("&","&&");
            labSongCount.Text = (Ampclient.IndexList.Count + Ampclient.Playlist.Count).ToString();
        }

        void Ampclient_ServerDisconnected()
        {
            this.Close();
        }

        private void Ampclient_ClientsUpdated(string serverIP)
        {
            if (Visible)
            {
                string searchServer;
                if (Ampclient.ConnectedServer == null)
                {
                    searchServer = "<lobby>";
                }
                else
                {
                    searchServer = Ampclient.ConnectedServer.IPaddress;
                }
                if (serverIP == searchServer)
                {
                    lvClients.Items.Clear();
                    lvClients.BeginUpdate();
                    foreach (Client client in Ampclient.Clients.Items)
                        if (client.Server == searchServer)
                        {
                            ListViewItem lvClient = lvClients.Items.Add(client.Nickname);
                            lvClient.SubItems.Add(client.Host);
                        }
                    lvClients.EndUpdate();
                }
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Ampclient.ServerConnected) Ampclient.ConnectedServer = null;
            JoinForm.Show();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbPlaylist.SelectedIndex > -1)
            {
                Ampclient.RemovePlayListItem((MusicInfo)lbPlaylist.SelectedItem);
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (lbPlaylist.SelectedIndex > 0)
            {
                Ampclient.Playlist.MoveUp(lbPlaylist.SelectedIndex);
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if ((lbPlaylist.SelectedIndex > -1) &&(lbPlaylist.SelectedIndex < (Ampclient.Playlist.Count - 1)))
            {
                Ampclient.Playlist.MoveDown(lbPlaylist.SelectedIndex);
            }
        }

        private void lbPlaylist_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = (lbPlaylist.SelectedIndex > -1);
            btnUp.Enabled = (lbPlaylist.SelectedIndex > 0);
            btnDown.Enabled = ((lbPlaylist.SelectedIndex > -1) && (lbPlaylist.SelectedIndex < lbPlaylist.Items.Count - 1));
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSelect select = new frmSelect(Ampclient.IndexList.Items);
            select.ItemSelectedAsLast += new frmSelect.ItemsSelectedEvent(select_ItemSelectedAsLast);
            select.ItemSelectedAsFirst += new frmSelect.ItemsSelectedEvent(select_ItemSelectedAsFirst);
            select.ShowDialog();
        }

        void select_ItemSelectedAsFirst(MusicInfo Selection)
        {
            Ampclient.InsertPlayListItem(Selection);
        }

        void select_ItemSelectedAsLast(MusicInfo Selection)
        {
            Ampclient.AddPlayListItem(Selection);
        }

        private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar == 'j')) btnAdd_Click(sender, e);
        }

        private void btnEnlarge_Click(object sender, EventArgs e)
        {
            this.Enlarged = (!this.Enlarged);
            lbPlaylist.Focus();
        }

        private void btnSkipVote_Click(object sender, EventArgs e)
        {
            if (Ampclient != null) Ampclient.SkipVote = true;
        }

        private void niLanAmp_Click(object sender, EventArgs e)
        {
/*
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                this.Visible = false;
            }
*/
            this.Visible = (!this.Visible);
        }
        private void lvClients_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvClients.SelectedIndices.Count > 0)
            {
                Client selectedClient = (Client)Ampclient.Clients.Items[lvClients.SelectedIndices[0]]; ;
                labClientName.Text = selectedClient.Nickname;
                labClientHost.Text = selectedClient.Hostname;
                labClientIP.Text = selectedClient.IPaddress;
                labClientVersion.Text = selectedClient.Version;
            }
        }

        private void niLanAmp_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.Activate();
        }

    }
}